#pragma once

#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

// #define Myself 1   //编译的时候可以 用-DMyself

using namespace std;

const string blank_sep = " ";
const string protocal_sep = "\n";

//"x op y" ->
//"len"/n"x op y"/n
std::string Encode(string &content)
{
    int len = content.size();
    string package = to_string(len);
    package += protocal_sep;
    package += content;
    package += protocal_sep;
    return package;
}

//"len"/n"x op y"/n ->  解析可能错误
//"x op y"
bool Decode(string &package, string *content)
{
    int pos = package.find(protocal_sep);
    if (pos == string::npos)
        return false;

    string len_str = package.substr(0, pos);
    int len = stoi(len_str);

    int to_len = len_str.size() + 2 + len;
    if (package.size() < to_len)
        return false;

    *content = package.substr(pos + 1, len); // 位置 加 长度

    package.erase(0, to_len); // 解析的这一段，从缓冲区里去掉
    return true;
}

class Request
{
public:
    Request(int x, int y, char op) : x_(x), y_(y), op_(op)
    {
    }

    Request()
    {
    }

    ~Request()
    {
    }

public:
    bool Serialize(string *content) // 序列化为 字节流
    {
#ifdef Myself
        string s = to_string(x_);
        s += blank_sep;
        s += op_;
        s += blank_sep;
        s += to_string(y_);

        *content = s;
        return true;
#else
        Json::Value root;

        root["x"] = x_;
        root["y"] = y_;
        root["op"] = op_;

        Json::FastWriter w;
        *content = w.write(root);
        return true;
#endif
    }

    bool Deserialize(string &in) // 反序列化 将字节流解析
    {
#ifdef Myself
        int l = in.find(blank_sep);
        if (l == string::npos)
            return false;
        string x = in.substr(0, l); // 左闭，右开

        int r = in.rfind(blank_sep);
        if (r == string::npos)
            return false;
        string y = in.substr(r + 1);

        if (l + 2 != r)
            return false;

        x_ = stoi(x);
        y_ = stoi(y);
        op_ = in[l + 1];
        return true;
#else
        Json::Reader R;
        Json::Value root;

        bool ret = R.parse(in.c_str(), root);
        if (!ret)
            return false;
        x_ = root["x"].asInt();
        y_ = root["y"].asInt();
        op_ = root["op"].asInt();
        return true;

#endif
    }

    void Printf()
    {
        cout << "任务请求" << x_ << op_ << y_ << "=?" << endl;
    }

public:
    int x_;
    int y_;
    char op_;
};

class Respone
{
public:
    Respone(int res, int c) : result_(res), code_(c)
    {
    }

    Respone()
    {
    }
    ~Respone()
    {
    }

public:
    bool Serialize(string *out) // 序列化 形成字节流
    {
#ifdef Myself
        string s = to_string(result_);
        s += blank_sep;
        s += to_string(code_);
        *out = s;
        return true;
#else
        Json::Value root;

        root["result"] = result_;
        root["code"] = code_;

        Json::FastWriter w;
        *out = w.write(root);
        return true;
#endif
    }

    bool Deserialize(string &in) // 反序列化 字节流解析
    {
#ifdef Myself
        int pos = in.find(blank_sep);
        if (pos == string::npos)
            return false;
        string x = in.substr(0, pos);
        int result = stoi(x);

        string y = in.substr(pos + 1);
        int code = stoi(y);

        result_ = result;
        code_ = code;
        return true;
#else
        Json::Reader R;
        Json::Value root;

        bool ret = R.parse(in.c_str(), root);
        if (!ret)
            return false;
        result_=root["result"].asInt();
        code_=root["code"].asInt();
        return true;
#endif
    }

    void Printf()
    {
        cout << "result " << result_ << "  code " << code_ << endl;
    }

public:
    int result_;
    int code_;
};